package com.sherlockHolmes.init.base.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateUtil {
    public static Date getNowTime()  {
        Date date = new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(date);
        try {
            return format.parse(format1);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static String getNowTime(String format)  {
        Date date = new Date();
        DateFormat format1=new SimpleDateFormat(format);
        return  format1.format(date);

    }

    public static Date offsetDay(Date nowTime, int i) {
        return cn.hutool.core.date.DateUtil.offsetDay(nowTime,i);
    }

    /**
     * 计算两个日期之间的天数
     * @param date1
     * @param date2
     * @return
     */
    public long calculateDaysBetweenDates(Date date1, Date date2) {
        LocalDate startDate = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        return ChronoUnit.DAYS.between(startDate, endDate);
    }
}
